%{
package gen;

import bison.LexToken;
import bison.Position;
%}

%define stype "Dynamic"
%parse-param  { symbolTable:Hash<Float> }
%locations

/* BISON Declarations */
%token   NUM        /* Simple double precision number   */
%token   VAR FNCT   /* Variable and Function            */
%token   ERROR      /* Returned parser error            */

%right '='
%left '-' '+'
%left '*' '/'
%left NEG     /* Negation--unary minus - used for defining precedence */
%right '^'    /* Exponentiation        */



%%

input:   /* empty */
        | input line
;

line:
          '\n'
        | exp '\n'   { output(" = "+$1); }
        | error '\n' { }
;

exp:      NUM                { $$ = $1;                         }
        | VAR                { $$ = symbolTable.get($1);        }
        | ERROR              { return yyerror($1,getCurrentLocation()); }
        | VAR '=' exp        { $$ = $3; symbolTable.set($1,$3); }
        | FNCT '(' exp ')'   { $$ = $1($3);                     }
        | exp '+' exp        { $$ = $1 + $3;                    }
        | exp '-' exp        { $$ = $1 - $3;                    }
        | exp '*' exp        { $$ = $1 * $3;                    }
        | exp '/' exp        { $$ = $1 / $3;                    }
        | '-' exp  %prec NEG { $$ = -$2;                        }
        | exp '^' exp        { $$ = Math.pow ($1, $3);          }
        | '(' exp ')'        { $$ = $2;                         }
;
/* End of grammar */
%%





